package com.gold.rabbitmqdemo.study1;

import com.gold.rabbitmqdemo.util.ConnectionUtils;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 简单队列-消费者  http://www.rabbitmq.com/tutorials/tutorial-one-java.html
 *
 */
public class SimpleConsumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取rabbitmq连接
        Connection connection= ConnectionUtils.getConnection();
        //创建一个channel频道
        Channel channel=connection.createChannel();

        //声明一个队列
        String queueName="test_simple_queue";
        channel.queueDeclare(queueName,false,false,false,null);

        //定义消费者，重写handleDelivery()
        Consumer consumer=new DefaultConsumer(channel){
            //获取到达消息
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                super.handleDelivery(consumerTag, envelope, properties, body);
                String message=new String(body,"UTF-8");
                System.out.println("simple consumer received message :" +message);
            }
        };
        //监听队列信息
        channel.basicConsume(queueName,consumer);

    }
}
